package cn.butcher21.view;

import cn.butcher21.dao.GoodsDao;
import cn.butcher21.pojo.MyGoods;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class StockController {

    @FXML
    private TableView<MyGoods> stockTable;
    @FXML
    private TableColumn<MyGoods, String> g_id;

    @FXML
    private TableColumn<MyGoods, String> g_name;

    @FXML
    private TableColumn<MyGoods, Integer> g_count;

    @FXML
    private TableColumn<MyGoods, Double> g_price;

    @FXML
    private TableColumn<MyGoods, Double> g_cost;

    @FXML
    private TableColumn<MyGoods, String> g_inMan;

    @FXML
    private Button inBtn;

    @FXML
    private TextField id;

    @FXML
    private TextField name;

    @FXML
    private TextField count;

    @FXML
    private TextField price;

    @FXML
    private TextField cost;

    @FXML
    private Button clearGoodsBtn;

    @FXML
    private Button delGoodsBtn;

    @FXML
    private Button updateGoodsBtn;

    @FXML
    private TextField keyword;

    @FXML
    private Button searchBtn;

    @FXML
    private Label goodsTips;

    LoginController login = (LoginController) Context.rootController.get(LoginController.class.getSimpleName());

    public void loadGoodsTableData() {
        //显示用户数据
        GoodsDao goodsDao = new GoodsDao();
        try {
            List<MyGoods> list = goodsDao.select();
            ObservableList<MyGoods> goods = FXCollections.observableArrayList();
            for (MyGoods goods1 : list) {
                goods.add(goods1);
            }
            g_id.setCellValueFactory(new PropertyValueFactory<MyGoods, String>("id"));
            g_name.setCellValueFactory(new PropertyValueFactory<MyGoods, String>("name"));
            g_cost.setCellValueFactory(new PropertyValueFactory<MyGoods, Double>("cost"));
            g_price.setCellValueFactory(new PropertyValueFactory<MyGoods, Double>("price"));
            g_count.setCellValueFactory(new PropertyValueFactory<MyGoods, Integer>("count"));
            g_inMan.setCellValueFactory(new PropertyValueFactory<MyGoods, String>("inMan"));
            stockTable.setItems(goods);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void initialize() {
        loadGoodsTableData();
    }


    @FXML
    void searchGoods(ActionEvent event) throws SQLException, IOException, ClassNotFoundException {


        String keywordText = keyword.getText();
        System.out.println("搜索" + keywordText);
        GoodsDao goodsDao = new GoodsDao();
        List<MyGoods> list = goodsDao.select(keywordText);
        ObservableList<MyGoods> goods = FXCollections.observableArrayList();
        for (MyGoods goods1 : list) {
            goods.add(goods1);
        }
        g_id.setCellValueFactory(new PropertyValueFactory<MyGoods, String>("id"));
        g_name.setCellValueFactory(new PropertyValueFactory<MyGoods, String>("name"));
        g_cost.setCellValueFactory(new PropertyValueFactory<MyGoods, Double>("cost"));
        g_price.setCellValueFactory(new PropertyValueFactory<MyGoods, Double>("price"));
        g_count.setCellValueFactory(new PropertyValueFactory<MyGoods, Integer>("count"));
        g_inMan.setCellValueFactory(new PropertyValueFactory<MyGoods, String>("inMan"));
        stockTable.setItems(goods);
    }


    @FXML
    void delGoods(ActionEvent event) {
        if (id.getText().equals("")) {
            goodsTips.setText("没有商品编号我删啥呀≧︿≦");
        } else {
            GoodsDao goodsDao = new GoodsDao();
            try {
                goodsDao.delete(id.getText());
                goodsTips.setText(id.getText() + "号商品删除成功≧０≦");
                clearGoodsLine(event);
                loadGoodsTableData();
            } catch (SQLException e) {
                e.printStackTrace();
                goodsTips.setText("糟了，失败了≧︿≦");
            } catch (IOException e) {
                e.printStackTrace();
                goodsTips.setText("糟了，失败了≧︿≦");
            } catch (ClassNotFoundException e) {
                goodsTips.setText("糟了，失败了≧︿≦");
                e.printStackTrace();
            }
        }

    }

    @FXML
    void clearGoodsLine(ActionEvent event) {
        id.setText("");
        name.setText("");
        count.setText("");
        price.setText("");
        cost.setText("");
    }

    @FXML
    void intoStock(ActionEvent event) {
        if (id.getText().equals("") | name.getText().equals("") | price.getText().equals("") | count.getText().equals("") | cost.getText().equals("")) {
            goodsTips.setText("商品信息要填写完整呀≧︿≦");
        } else {
            GoodsDao goodsDao = new GoodsDao();
            MyGoods goods = new MyGoods();
            goods.setId(id.getText());
            goods.setName(name.getText());
            goods.setCount(Integer.parseInt(count.getText()));
            goods.setPrice(Double.parseDouble(price.getText()));
            goods.setCost(Double.parseDouble(cost.getText()));
            goods.setInMan(login.getUsername().getText());
            try {
                goodsDao.add(goods);
                goodsTips.setText(id.getText() + "号商品添加成功≧０≦");
                clearGoodsLine(event);
                loadGoodsTableData();
            } catch (SQLException e) {
                goodsTips.setText(id.getText() + "号商品已存在，您可以更新或删除≧︿≦");
                e.printStackTrace();
            } catch (IOException e) {
                goodsTips.setText(id.getText() + "号商品已存在，您可以更新或删除≧︿≦");
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                goodsTips.setText(id.getText() + "号商品已存在，您可以更新或删除≧︿≦");
                e.printStackTrace();
            }
        }

    }

    @FXML
    void updateGoods(ActionEvent event) {
        if (id.getText().equals("") | name.getText().equals("") | count.getText().equals("")) {
            goodsTips.setText("至少要填编号、名字、数量≧︿≦");
        } else {
            GoodsDao goodsDao = new GoodsDao();
            MyGoods goods = new MyGoods();
            goods.setId(id.getText());
            goods.setName(name.getText());
            goods.setCount(Integer.parseInt(count.getText()));
            goods.setInMan(login.getUsername().getText());
            try {
                goodsDao.update(goods);
                goodsTips.setText(id.getText() + "号商品更新成功≧０≦");
                clearGoodsLine(event);
                loadGoodsTableData();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    @FXML
    void getGoodsSelect(MouseEvent event) {
        MyGoods goods = stockTable.getSelectionModel().getSelectedItem();
        if (goods != null) {
            id.setText(goods.getId());
            name.setText(goods.getName());
            count.setText(String.valueOf(goods.getCount()));
            price.setText(String.valueOf(goods.getPrice()));
            cost.setText(String.valueOf(goods.getCost()));
        }
    }

}
